﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.Linq;
using System.Linq;

namespace InvoiceInvoker.Logic.Repositories
{
	public class RegisteredProductRepository : InvoiceInvoker.Logic.RepositoryInterfaces.IRegisteredProductRepository
	{
		private string _connectionString;
		private int _registeredSellerId;

		public RegisteredProductRepository(int registeredSellerId)
		{
			_connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
			_registeredSellerId = registeredSellerId;
		}

		public List<RegisteredProduct> GetAll()
		{
			using (MainDataContext dataContext = new MainDataContext(_connectionString))
			{
				var query = from product in dataContext.RegisteredProducts
							where product.RegisteredSellerId == _registeredSellerId
							orderby product.Name ascending
							select product;
				return query.ToList();
			}
		}

		public List<RegisteredProduct> GetByName(string name)
		{
			using (MainDataContext dataContext = new MainDataContext(_connectionString))
			{
				var query = from product in dataContext.RegisteredProducts
							where product.RegisteredSellerId == _registeredSellerId &&
								  product.Name.ToLower().StartsWith(name.ToLower())
							orderby product.Name ascending
							select product;
				return query.ToList();
			}
		}

		public List<RegisteredProduct> GetByExpression(Func<RegisteredProduct, bool> expression)
		{
			using (MainDataContext dataContext = new MainDataContext(_connectionString))
			{
				var mainQuery = from product in dataContext.RegisteredProducts
								where product.RegisteredSellerId == _registeredSellerId
								orderby product.Name ascending
								select product;
				return mainQuery.Where(expression).ToList();
			}
		}

		public RegisteredProduct GetById(int id)
		{
			using (MainDataContext dataContext = new MainDataContext(_connectionString))
			{
				return dataContext.RegisteredProducts.SingleOrDefault(product => product.Id == id);
			}
		}

		public void Add(RegisteredProduct item)
		{
			using (MainDataContext dataContext = new MainDataContext(_connectionString))
			{
				dataContext.RegisteredProducts.InsertOnSubmit(item);
				dataContext.SubmitChanges();
			}
		}

		public void Remove(int itemId)
		{
			DataLoadOptions loadOptions = new DataLoadOptions();
			loadOptions.LoadWith<RegisteredProduct>(x => x.InvoicePatternRegisteredProducts);

			using (MainDataContext dataContext = new MainDataContext(_connectionString))
			{
				var query = from product in dataContext.RegisteredProducts where product.Id == itemId select product;
				RegisteredProduct toRemove = query.SingleOrDefault();

				foreach (var item in toRemove.InvoicePatternRegisteredProducts)
					dataContext.InvoicePatternRegisteredProducts.DeleteOnSubmit(item);

				dataContext.RegisteredProducts.DeleteOnSubmit(toRemove);
				dataContext.SubmitChanges();
			}
		}

		public void Update(RegisteredProduct item)
		{
			using (MainDataContext dataContext = new MainDataContext(_connectionString))
			{
				dataContext.RegisteredProducts.Attach(item, GetById(item.Id));
				dataContext.SubmitChanges();
			}
		}
	}
}
